package com.fhzn.auth.web.request;

import com.fhzn.auth.entity.Role;
import com.fhzn.auth.entity.RoleRelation;
import com.fhzn.auth.entity.RoleResource;
import com.fhzn.auth.enums.CommonStatusEnum;
import com.fhzn.commons.toolkit.exception.BuzException;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/26
 */

@Data
@EqualsAndHashCode(callSuper = true)
public class RoleOpRequest extends BaseRequest {
    private Integer id;

    private String name;

    private String code;

    private String sign;

    private Integer status;

    private String comment;

    private String appCode;

    /**
     * 此角色能任命的角色id集合,逗号隔开
     */
    private String canAppointRoles;

    /**
     * 角色对应的资源id集合,逗号隔开
     */
    private String resources;

    private String users;

    /**
     * 角色关联群组id(默认是关联用户组)
     */
    private Integer groupId;

    public void checkAdd() {
        this.checkCommon();
        if (StringUtils.isBlank(appCode)) {
            throw new BuzException("appCode不能为空");
        }
    }

    public void checkUpdate() {
        if (id == null) {
            throw new BuzException("id不能为空");
        }
        checkCommon();
    }

    public void checkCommon() {
        this.checkBaseParams();
        if (status == null || !CommonStatusEnum.contains(status)) {
            throw new BuzException("状态位不正确");
        }
    }


    /**
     * 生成发往服务端的query对象
     *
     * @return
     */
    public Role genServiceParam() {
        Role role = new Role();
        role.setModifier(this.getHandler());
        role.setSign(this.getSign());
        role.setAppCode(this.getAppCode());
        role.setCode(this.getCode());
        role.setId(this.getId());
        role.setName(this.getName());
        role.setComment(this.getComment());
        role.setStatus(this.getStatus());
        role.setGroupId(groupId);
        return role;
    }

    public Role addGenServiceParam() {
        Role role = genServiceParam();
        role.setCreator(this.getHandler());
        return role;
    }


    public List<RoleResource> genRoleResourceList() {
        List roleResources = Lists.newArrayList();
        if (StringUtils.isNotBlank(resources)) {
            String[] resIds = resources.split(",");
            for (String resId : resIds) {
                RoleResource roleResource = new RoleResource();
                roleResource.setCreator(this.getHandler());
                roleResource.setModifier(this.getHandler());
                roleResource.setResourceId(Integer.parseInt(resId));
                roleResource.setRoleId(this.getId());
                roleResources.add(roleResource);
            }
        }
        return roleResources;
    }

    public List<RoleRelation> genRoleRelationList() {
        List<RoleRelation> roleRelations = Lists.newArrayList();
        if (StringUtils.isNotBlank(canAppointRoles)) {
            String[] appointIds = canAppointRoles.split(",");
            for (String appointId : appointIds) {
                RoleRelation roleRelation = new RoleRelation();
                roleRelation.setCreator(this.getHandler());
                roleRelation.setModifier(this.getHandler());
                roleRelation.setRoleId(Integer.parseInt(appointId));
                roleRelation.setSuperId(this.getId());
                roleRelations.add(roleRelation);
            }
        }
        return roleRelations;
    }
}
